import {Injectable} from '@angular/core';
import {Building} from '../types/resp/Building';
import {BuildingForm} from '../types/req/BuildingForm';
import {Result} from '../types/result';
import MockApi from '../api/mockApi';
import {map, Observable} from 'rxjs';
import type {Response} from '../types/response';
import {Page} from '../types/page';
import {BuildingQueryForm} from '../types/req/BuildingQueryForm';

@Injectable({
  providedIn: 'root'
})
export class BuildingService {
  buildings: Building[] = [
    {id: 1, buildingName: '北区1栋', address: '熏衣区望北街1号', floorCount: 42, roomCount: 256, createTime: '2024-01-01 12:12:12', modifyTime: '2024-02-02 13:13:13'},
    {id: 2, buildingName: '北区2栋', address: '熏衣区望北街2号', floorCount: 43, roomCount: 267, createTime: '2024-02-02 12:12:12', modifyTime: '2024-03-03 13:13:13'},
    {id: 3, buildingName: '北区3栋', address: '熏衣区望北街3号', floorCount: 44, roomCount: 278, createTime: '2024-03-03 12:12:12', modifyTime: '2024-04-04 13:13:13'},
    {id: 4, buildingName: '北区4栋', address: '熏衣区望北街4号', floorCount: 45, roomCount: 289, createTime: '2024-04-04 12:12:12', modifyTime: '2024-05-05 13:13:13'}
  ]
  constructor() {
  }

  save (buildingForm: BuildingForm): Observable<Result<void>> {
    console.log('buildingApi save params', buildingForm)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }

  find (buildingQueryForm: BuildingQueryForm): Observable<Result<Page<Building>>> {
    console.log('buildingApi find params', buildingQueryForm)
    return MockApi.queryPageSuccessfully(this.buildings).pipe(
      map((response: Response<Result<Page<Building>>>) => response.data)
    );
  }

  findById (id: number): Observable<Result<Building>> {
    console.log('buildingApi findById id', id)
    let target = {}
    for (const a of this.buildings) {
      if (a.id === id) {
        target = a
      }
    }
    return MockApi.operateSuccessfullyWithData(target).pipe(
      map((response: Response<Result<Building>>) => response.data)
    );
  }

  del (id: number): Observable<Result<void>> {
    console.log('buildingApi del id', id)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }
}
